package com.example.myanimator.mcanvas

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

/**
 * @author tgw
 * @date 2021/4/1
 * @describe 画布旋转
 */
class CanvasRotateView @JvmOverloads constructor( //相当于Java中的构建了三个构造函数
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private val mPaint: Paint
    private val mBmp: Bitmap? = null
    private val mBmpCanvas: Canvas? = null


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.let {
            it.saveLayer(0f, 0f, width.toFloat(), height.toFloat(), null, Canvas.ALL_SAVE_FLAG)
            it.save()
            it.rotate(40f)
            it.drawRect(Rect(100, 100, 200, 200), mPaint)
            it.restore()
        }
//        canvas.saveLayer(0f, 0f, width.toFloat(), height.toFloat(), null, Canvas.ALL_SAVE_FLAG)
//        canvas.save()
//        canvas.rotate(40f)
//        canvas.drawRect(Rect(100, 100, 200, 200), mPaint)
//        canvas.restore()
        mPaint.color = Color.BLACK
        canvas.drawRect(Rect(100, 100, 200, 200), mPaint)
    }

    init {
        setLayerType(LAYER_TYPE_SOFTWARE, null) //禁用加速
        mPaint = Paint()
        mPaint.color = Color.RED
    }
}